﻿using System;
using System.Data;
using System.Configuration;
using Dcms.Common;
using Dcms.BLL;
using Dcms.Model;
using System.Collections;
using WebApp.AppCode;

public partial class Manage_RoleManage_UserInfo : System.Web.UI.Page
{
    private int UserID = 0;
    public CompetenceLib CL = new CompetenceLib();
    Dcms_AdminBLL aBll = new Dcms_AdminBLL();
    Dcms_AdminModel aModel = new Dcms_AdminModel();
    protected void Page_Load(object sender, EventArgs e)
    {
        CL.IsLogin();

        if (Request["AdmID"] != null)
        {
            UserID = Convert.ToInt32(Request["AdmID"]);
        }

        if (!IsPostBack)
        {
            aModel = aBll.GetModelByID(UserID);

            lit_AdminEmail.Text = aModel.Admin_Email;
            lit_AdminLastIp.Text = aModel.Admin_LastIp;
            lit_AdminLastTime.Text = aModel.Admin_LastTime.ToString("yyyy-MM-dd");
            lit_AdminLoginTimes.Text = aModel.Admin_LoginTimes.ToString();
            lit_AdminName.Text = aModel.Admin_Name;


            ArrayList CoderAl = aBll.GetCoders();
            if (!CoderAl.Contains(aModel.Admin_Name))
            {
                    BinderRole();
            }
            else
            {
                lit_Role.Text = "系统默认管理员";
                this.ddlRole.Visible = false;
                this.btnRole.Visible = false;
            }
        }
    }
    protected void SetErrorMsg(String Msg)
    {
        Mesg.Text = "<div id=\"msgCard\"><div class=\"error\"><div class=\"error-content\">" + Msg + "</div></div></div>";
    }
    protected void SetOKMsg(String Msg)
    {
        Mesg.Text = "<div id=\"msgCard\"><div class=\"accurate\"><div class=\"accurate-content\">" + Msg + "</div></div></div>";
    }
    private void BinderRole()
    { 
        aModel = aBll.GetModelByID(UserID);
        Dcms_RoleBLL rBll = new Dcms_RoleBLL();
        this.lit_Role.Visible = false;
        this.ddlRole.DataSource = rBll.GetModelList();
        this.ddlRole.DataTextField = "Role_Name";
        this.ddlRole.DataValueField = "Role_Id";
        this.ddlRole.DataBind();
        
        //除排设计组
        string strDesignRoleID = ConfigurationManager.AppSettings["DesignRoleID"];
        Dcms_RoleModel DesignRoleModel = rBll.GetModelByID(Convert.ToInt32(strDesignRoleID));
        this.ddlRole.Items.Remove(new System.Web.UI.WebControls.ListItem(DesignRoleModel.Role_Name, DesignRoleModel.Role_Id.ToString()));
        ddlRole.SelectedValue = aModel.Admin_RoleId.ToString();
    }

    protected void btnRole_Click(object sender, EventArgs e)
    {
        int RoleId = Convert.ToInt32(ddlRole.SelectedValue);
        aBll.UpdateRole(UserID, RoleId);
        SetOKMsg("用户角色设置成功！");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserList.aspx?id=" + strPermissionID ,false);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string PassWord= "e10adc3949ba59abbe56e057f20f883e";
        if (aBll.UpdatePwd(UserID, PassWord) > 0)
        {
            SetOKMsg("用户密码恢复成功，密码为123456！");
        }
        else
        {
            SetErrorMsg("用户密码恢复失败！");
        }
    }

    protected string strPermissionID
    {
        get
        {
            return Request.QueryString["PermissionID"] + "";
        }
    }
}
